package com.byxy.xyqas.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.byxy.xyqas.dao.ForumDao;
import com.byxy.xyqas.entity.Forum;
import com.byxy.xyqas.service.ForumService;
import com.byxy.xyqas.vo.ForumVO;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class ForumServiceImpl implements ForumService {
	@Autowired
	private ForumDao forumDao;

	public List<ForumVO> findAll() {
		List<Forum> forums = forumDao.findAll();
		List<ForumVO> forumVOs = new ArrayList<>();
		for (Forum f : forums) {
			ForumVO vo = new ForumVO();
			BeanUtils.copyProperties(f, vo);
			forumVOs.add(vo);
		}
		return forumVOs;
	}

	public ForumVO get(Integer forumId) {
		Forum f = forumDao.getOne(forumId);
		ForumVO vo = new ForumVO();
		BeanUtils.copyProperties(f, vo);
		return vo;
	}

	public void delete(Integer forumId) {
		forumDao.deleteById(forumId);
	}

	@Override
	public void update(ForumVO forum) {
		Forum f = new Forum();
		f.setForumId(forum.getForumId());
		f.setForumName(forum.getForumName());
		forumDao.save(f);
	}

	@Override
	public void add(ForumVO forum) {
		Forum f = new Forum();
		f.setForumName(forum.getForumName());
		forumDao.save(f);
	}
}
